﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NPMonitor.Implementation;
using NPMonitor.DataModel.Report;
using System.Web.UI.HtmlControls;

namespace NPMonitor.Web.Reports
{
    public partial class HttpRequetFrames : BasePage
    {
        public override string FunctionName
        {
            get { return FunctionEnum.HttpRequest.ToString(); }
        }
        protected override void OnPreRender(EventArgs e)
        {
            rfvJob.ErrorMessage = string.Format(ValidationString.PleaseSelect, "Job");
            rfvLocation.ErrorMessage = string.Format(ValidationString.PleaseSelect, "Location");
            cvScenarioNO.ErrorMessage = string.Format(ValidationString.PleaseSelect, "Scenario");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControl();
                InitPage();
            }
            BindScenarios();
        }
        protected void InitPage()
        {
            DateTime now = DateTime.UtcNow;
            ucTimePicker.BeginDate = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0).ToDateHourString();
            ucTimePicker.EndDate = now.ToDateHourString();
            ucTimePicker.ReportType = ReportType.Daily.ToString();
        }
        protected void BindControl()
        {
            var jobList = StoreManager.Current.JobDefinitionStore.GetAllJobDefinitions(0, -1);
            ddlJob.BindDataSource(jobList, "JobName", "JobId");

            var locationList = StoreManager.Current.MachineStore.GetAllMachinePool();
            ddlLocation.BindDataSource(locationList, "PoolName", "PoolId");
        }
        protected void BindScenarios()
        {
            int jobId;
            if (int.TryParse(ddlJob.SelectedValue, out jobId))
            {
                var scenarioList = StoreManager.Current.ReportStore.GetScenarioNoByJobId(jobId);
                ddlSenario.BindDataSource(scenarioList, "ScenarioName", "ScenarioNO");
                ddlSenario.SelectedValue = hfScenarioNO.Value;
            }
            else
            {
                ddlSenario.BindDataSource(null, null, null);
            }
        }

        protected void btnViewReport_Click(object sender, EventArgs e)
        {
            List<ReportData> dataList = StoreManager.Current.ReportStore
                .GetDailyReportDataByCondition(hfScenarioNO.Value, ucTimePicker.BeginDate, ucTimePicker.EndDate, ddlLocation.SelectedValue);
            ucReport.BulidChart(dataList, null);

            ucReport.DisableTable = true;

            BindTable(dataList);
        }

        protected void BindTable(List<ReportData> datalist)
        {
            if (datalist != null && datalist.Count > 0)
            {
                HtmlTable table = new HtmlTable();
                table.CellSpacing = 1;
                table.CellPadding = 0;
                table.Border = 0;

                HtmlTableRow firstRow = new HtmlTableRow();

                HtmlTableCell firstCell = new HtmlTableCell { InnerText = "In Second" };

                firstRow.Cells.Add(firstCell);

                foreach (var data in datalist)
                {
                    var cell = new HtmlTableCell();
                    cell.InnerText = data.Legend;
                    cell.Attributes.Add("style", "background-color:#0978c0; color:#fff;font-weight:bold;");
                    firstRow.Cells.Add(cell);
                }

                var lastCell = new HtmlTableCell();
                lastCell.InnerText = "Actions";
                lastCell.Attributes.Add("style", "background-color:#0978c0; color:#fff;font-weight:bold;");
                firstRow.Cells.Add(lastCell);

                table.Rows.Add(firstRow);


                foreach (var item in datalist)
                {
                    foreach (var point in item.list)
                    {
                        string beginTime = point.BeginTime;
                        HtmlTableRow contentRow = new HtmlTableRow();
                        contentRow.Cells.Add(new HtmlTableCell
                        {
                            InnerText = beginTime
                        });
                        //contentRow = new HtmlTableRow();
                        contentRow.Cells.Add(new HtmlTableCell
                        {
                            InnerText = point.AverageTime != 0 ? string.Format("{0:N3}", point.AverageTime) : "-"
                        });
                        contentRow.Cells.Add(new HtmlTableCell
                        {
                            InnerHtml = string.Format(
                            "<a target='_blank' href='HttpActionList.aspx?ScenarioNo={0}&BeginTime={1}'>Actions</a>"
                            , hfScenarioNO.Value.Trim(), beginTime)
                        });

                        table.Rows.Add(contentRow);
                    }
                }


                phTable.Controls.Add(table);
            }
        }


    }
}